package com.cwt_taxi_xa;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.cwt_taxi_xa.protocol.MCUUtil;
import com.cwt_taxi_xa.service.FloatService;
import com.cwt_taxi_xa.util.AppLog;

import java.io.File;
import java.io.FileOutputStream;

public abstract class CWTBaseActivity extends SwipeBackActivity{

	/**
	 * 当前界面是否开启右滑finish的功能
	 * true:开启		false:不开启
	 */
	public void openSwipeBack(boolean openState) {
		// TODO Auto-generated method stub
		super.openSwipeBack(openState);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		AppLog.savelog("界面变动：   "+this.getClass().getName());
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MainApplication.getInstance().topActivity = this;
		AppLog.savelog("界面变动：   "+this.getClass().getName());
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		AppLog.savelog("界面变动：   "+this.getClass().getName());
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		AppLog.savelog("界面变动：   "+this.getClass().getName());
	}

	long lastrefreshtime = 0;
	public static int refreshcount = 100;
	public void refreshLine() {
		if(refreshcount<1&&System.currentTimeMillis()-lastrefreshtime<120*1000) return;
		if(System.currentTimeMillis()-lastrefreshtime>8*1000){
			lastrefreshtime = System.currentTimeMillis();
			if(refreshcount<1){
				refreshcount = 3;
			}else{
				refreshcount--;
			}
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
						File f = new File("/data/eth0_setting.sh");
						if(f.exists()){
							f.delete();
						}
						f.createNewFile();
						FileOutputStream fos = new FileOutputStream(f);
						fos.write("#!/system/bin/sh\nbusybox ifconfig eth0 down".getBytes());
						fos.flush();
						fos.close();
						handler_line.sendEmptyMessage(1001);
						Thread.sleep(2000);
						if(f.exists()){
							f.delete();
						}
						f.createNewFile();
						fos = new FileOutputStream(f);
						fos.write("#!/system/bin/sh\nbusybox ifconfig eth0 up".getBytes());
						fos.flush();
						fos.close();
						handler_line.sendEmptyMessage(1001);


//						AppLog.saveTxt2("有线网断开重连");
//						AppLog.e("------------------->断开有线网重连");
					} catch (Exception e) {
//						e.printStackTrace();
//						AppLog.e("------------------->断开有线网重连失败");
					}
				}
			}).start();
		}
	}

	long time_sendpic = 0;

	public void sendpic(int type){
		if(System.currentTimeMillis()-time_sendpic<10*1000){
			return;
		}else{
			time_sendpic = System.currentTimeMillis();
		}
		FloatService.sendpictype = type;
		Intent intent = new Intent(FloatService.action_camera);
		intent.putExtra("type", 2);
		intent.putExtra("seq", 0);
		intent.putExtra("w", 640);
		intent.putExtra("h", 480);
		intent.putExtra("channelId", 2);
		MainApplication.topActivity.sendBroadcast(intent);
	}

	public  void showNormalDialog(){
		handler_line.sendEmptyMessage(1002);
	}

	public void myToast(String str){
		handler_line.sendMessage(handler_line.obtainMessage(1003, str));

	}
	public void init(String str){

	}
	//已下载好新版本，提示升级
	public void showLocaUpdata(String path){
		handler_line.sendMessage(handler_line.obtainMessage(1004, path));
//		handler_line.sendEmptyMessage(1004);
	}

	Handler handler_line = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if(msg.what==1001){
				sendBroadcast(new Intent("android.intent.action.yho.set.ethnet"));
			}
			if(msg.what==1002){
				final AlertDialog.Builder normalDialog =
						new AlertDialog.Builder(MainApplication.topActivity);
//			        normalDialog.setIcon(R.drawable.icon_dialog);
				normalDialog.setTitle("MCU升级");
				normalDialog.setMessage("本次升级会引起大屏重启，现在是否方便？");
				normalDialog.setPositiveButton("确定升级",
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								MCUUtil.UpV();
							}
						});
				normalDialog.setNegativeButton("不方便，等会再升级", null);
				// 显示
				normalDialog.show();
			}
			if(msg.what==1003){
				String str = (String) msg.obj;
				Toast.makeText(MainApplication.topActivity, str, 1000).show();
				init(str);
			}

			if(msg.what==1004){
				//寻找本地文件中最新版本，删除其余低版本，弹出对话框升级
//				File file = new File(Environment.getExternalStorageDirectory(), "新版本-"+v+".apk");
//				File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
//				if(file.exists()){
//					File[] fs = file.listFiles();
//					String path = "";
//					int path_v = 0;
//					for (int i = 0; i < fs.length; i++) {
//						if(fs[i].getName().contains("新版本")){
//							if(fs[i].getName().split("-").length==2){
//								String str = fs[i].getName().split("-")[1].replace(".apk", "");
//								try {
//									int v = Integer.parseInt(str);
//									if(v>path_v){
//										path_v = v;
//										path = fs[i].getAbsolutePath();
//									}
//								} catch (Exception e) {
//								}
//							}
//						}
//					}
//					if(path_v>0){
//						Intent intent = new Intent(Intent.ACTION_VIEW);
//						intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
//						MainApplication.topActivity.startActivity(intent);
//					}
//				}
				String path = (String) msg.obj;
				Intent intent = new Intent(Intent.ACTION_VIEW);
				intent.setDataAndType(Uri.fromFile(new File(path)), "application/vnd.android.package-archive");
				MainApplication.topActivity.startActivity(intent);
			}
		};
	};


	/**
	 * 广播签到、营运等状态信息
	 * type:
	 * 1-司机签到
	 * 2-司机签退
	 * 3-空转重（乘客上车）
	 * 4-重转空（乘客下车）
	 */
	public String InfoAction = "com.cwt.sendInfo2DIDA";
	public void sendInfoBroadcast(int type, byte[] info){
		Intent intent = new Intent(InfoAction);
		intent.putExtra("type", type);
		intent.putExtra("info", info);
		intent.putExtra("device", 1);
		sendBroadcast(intent);
	}






	/**
	 * 设置右上角标识状态
	 * 1-网络
	 * 2-平台
	 * 3-定位
	 * @return
	 */
	public abstract void setStatus(int index, boolean status);


	/**
	 * type:
	 * 1001-刷卡签到-参数String
	 * 1002-刷新时间-参数String
	 * 1003-计价器请求开机
	 * 1004-计价器开机
	 * 1005-计价器请求关机
	 * 1006-计价器关机
	 * 1007-关机刷卡
	 * 1008-单次运营开始
	 * 1009-报警按钮
	 * 1010-单次运营结束
	 * 1011-发送计价器心跳，用于判断是否同步
	 * 1012-mcu上报的电压
	 *
	 * 1014-关闭倒车影像
	 * 1015-mcu查版本
	 *
	 * 1016-模拟刷tsm卡
	 *
	 * 1017-串口数据测试
	 * 1018-串口每帧数据
	 *
	 * 404---测试-acc接上
	 * 789---FM搜台结果返回
	 * 10086-测试-写卡界面截取数据
	 * 10087-测试-tsam读卡测试1
	 * 10088-测试-tsam读卡测试2
	 * 10089-测试-tsam读卡测试3
	 * @param type
	 * @param obj
	 */
	public abstract void sendMsg(int type, Object obj);

}
